﻿using System;
using System.IO;
using System.ServiceModel;
using System.Threading;

namespace ajayumi.develop.win32.Transport.Client.WinForm
{
    ///FileListener.cs 文件监听类
    ///Author   : 黄乙冬
    ///Date     : 2011-11-08
    internal class FileListener : ListenBase
    {

        #region Properties
        /// <summary>
        /// 备份文件夹
        /// </summary>
        public string BackupDir { get; private set; }

        /// <summary>
        /// 备份文件名
        /// </summary>
        public string BakName { get; private set; }

        /// <summary>
        /// 是否连接服务器
        /// </summary>
        public bool IsConnected
        {
            get
            {
                bool result = false;
                //MCUServiceClient client = new MCUServiceClient();
                //try
                //{
                //    client.IsConnected();
                //    result = true;
                //    client.Close();
                //}
                //catch (FaultException ex)
                //{
                //    this.DisplayMsg(string.Format("{1} 连接服务器发生错误。错误信息：\n{0}\n", ex.Message, DateTime.Now));
                //    (client as ICommunicationObject).Abort();
                //}
                //catch (TimeoutException ex)
                //{
                //    this.DisplayMsg(string.Format("{1} 连接服务器发生错误。错误信息：\n{0}\n", ex.Message, DateTime.Now));
                //    (client as ICommunicationObject).Abort();
                //}
                //catch (Exception ex)
                //{
                //    this.DisplayMsg(string.Format("{1} 连接服务器发生错误。错误信息：\n{0}\n", ex.Message, DateTime.Now));
                //    (client as ICommunicationObject).Abort();
                //}
                return result;
            }
        }

        public Action<FileInfo, string> UploadFile { get; set; }

        public Func<string> GetListenFile { get; private set; }

        #endregion


        public FileListener(Func<string> getListenFile, Action<string> displayMsg)
            : base(displayMsg)
        {
            this.GetListenFile = getListenFile;
        }

        protected override void ListenTask()
        {
            this.DisplayMsg(string.Format("{0} 监听文件 ……\r\n", DateTime.Now));
            string listenFile = this.GetListenFile();
            if (!File.Exists(listenFile))
            {
                this.DisplayMsg(string.Format("{0} {1} 文件不存在。\r\n", DateTime.Now, listenFile));
                return;
            }

            bool isLock = true;
            while (isLock)
            {
                try
                {
                    FileInfo file = new FileInfo(listenFile);
                    if (this.IsConnected && file.Exists && file.Length > 0)
                    {
                        //生成备份故障日志的文件夹
                        this.BackupDir = Path.Combine(Path.GetDirectoryName(listenFile), "Bak");
                        if (!Directory.Exists(this.BackupDir))
                        { Directory.CreateDirectory(this.BackupDir); }

                        //故障日志文件路径
                        this.BakName = string.Concat(DateTime.Now.ToString("yyyyMMddHHmmss"), ".LOG");
                        string bakPath = Path.Combine(this.BackupDir, this.BakName);

                        this.UploadFile(file, bakPath);
                    }

                    isLock = false;
                }
                catch (Exception ex)
                {
                    this.DisplayMsg(string.Format("{0} 访问 {1} 文件失败。\r\n{2}\r\n", DateTime.Now, listenFile,ex.Message));
                    Thread.Sleep(1000);
                }
            }
        }
    }
}
